﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
/*
 * 
 *   基本信息的处理
 * 
 */
namespace TRPOP.CMS.Template.GenerateCS
{
    /// <summary>
    /// 将cms标签替换为csharp语言
    /// </summary>
    public partial class ConvertTags
    {
        /// <summary>
        /// 解析cms标签
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="IsAddNote"></param>
        /// <returns></returns>
        public static string ConvertTag_CMS(ref string strSource, Match oMatch, string strSpaces, bool IsAddNote)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //解析CMS标签
            Match oCMSMatch =  oTags_CMS.Match(strSource, oMatch.Index);
            if (oCMSMatch.Success)
            {
                strReturn += strSpaces + string.Format(strTagAnayliseBegin, oCMSMatch.Value, Environment.NewLine);
                Dictionary<string, string> oDictionary = GetAttribute(oCMSMatch.Groups["attribute"].ToString());
                if (oDictionary.ContainsKey("action") && oDictionary.ContainsKey("return"))
                {
                    strReturn += strSpaces + "oAttributes.Clear();" + Environment.NewLine;
                    foreach (KeyValuePair<string, string> item in oDictionary)
                    {
                        strReturn += strSpaces + string.Format("oAttributes.Add(\"{0}\", {1});", item.Key.ToLower(), ConvertStringToCsharp(item.Value)) + Environment.NewLine;
                    }
                    strReturn += strSpaces + "SaveCMSTagData(oAttributes);" + Environment.NewLine;
                }
                strReturn += strSpaces + string.Format(strTagAnayliseEnd, oCMSMatch.Value, Environment.NewLine);

                strSource = strSource.Replace(oCMSMatch.Value, "");
            }
            else
            {
                strSource = strSource.Replace(oMatch.Value, "");
            }

            if (!strSource.IsEmpty() && strSource.Length > oMatch.Index + 1)
            {
                strSource = strSource.Substring(oMatch.Index);
            }
            return strReturn;
        }
        /// <summary>
        /// 将含有变量的字符串替换为chsarp中间代码
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string ConvertStringToCsharp(string strSource)
        {
            string strReturn = strSource.Replace("{", "[").Replace("}", "]").Trim();
            
            if (strReturn.Contains("[$") && strReturn.Contains("]"))
            {
                //找到每个变量
                Dictionary<string, string> oDicList = new Dictionary<string, string>();
                foreach (Match oTempMatch in oTags_AllTypeVar.Matches(strReturn))
                {
                    if (!oDicList.ContainsKey(oTempMatch.Value))
                    {
                        oDicList.Add(oTempMatch.Value,
                                        string.Format("GetTagValue(\"{0}\", \"{1}\", \"{2}\")", oTempMatch.Groups["DataSet"].Value.Trim(), oTempMatch.Groups["index"].Value.Trim(), oTempMatch.Groups["var"].Value.Trim())
                                    );
                    }
                }

                //将每个变量替换为解析函数，将其他的字符串替换为Csharp定义的字符串
                strReturn = "\"" + strReturn + "\"";
                foreach (KeyValuePair<string, string> item in oDicList)
                {
                    strReturn = strReturn.Replace(item.Key,
                                                 "\" + " + item.Value + " + \"");
                }

            }
            else
            {
                strReturn = "\"" + strReturn + "\"";
            }
            return strReturn;
        }
    }
}